<?php

class Drier extends Connect{
    
    
    /**
     * 
    */
    function addDrier($id, $habarid, $time, $elements, $type){        
        $sql = "INSERT INTO `".PREFIX_TABLE."drier` 
            (`user`, `habar`, `typehabar`, `time`, `elementhabar`)";
        if(!is_array($elements)) $elements = array();
        for($i = 0; $i < count($elements); $i++){
            $sql .= ($i == 0)? " VALUES  ": "" ;
            $sql .= "('".$id."', '".$habarid."', '".$type."', '".$time."', '".$elements[$i]['id']."')";
            $sql .= ($i < count($elements)-1)? ", " : "";
            #_echo(time()." - ".$time);
        }
        #_echo(count($elements));
        if($i == 0) return true;
        if(!$result = DBConnect::query($sql)){
            $this->_error(mysql_error());
            return false;
        }
        return true;#$this->complete(false, "Хабар добавлен"); 
    }
    
    
    
    /**
     * 
     * Возвращает то, что в сушилке
    */
    function  getDrier($id){
        
        $ingridient = new Ingredient();
        
        $sql = "SELECT * FROM `".PREFIX_TABLE."drier` 
                WHERE `user` = '".$id."'";        
        if(!$result = DBConnect::query($sql)){
            $this->_error(mysql_error());
            return false; 
            #$this->error(mysql_error());
        }
        $drier = array();
        $i = 0;
        Habars::getHabars();
        #Habars::$habars;
        
        $mantra = new Mantra($id);
        
        while($arr = DBConnect::getOne($result)){           
            #return Habars::$habars;
            #return $arr;
               #return Habars::$habars[$arr['habar']]['elements'][$arr['elementhabar']]['timedrying'];
            #if($this->checkHabarOffDrier($arr['time'], Habars::$habars[$arr['habar']]['timedrying'.$arr['elementhabar']])){            
            
            /**
             * Действует ли заклинание. Обновить $arr
            */
            $arr['timedrying'] = Habars::$habars[$arr['habar']]['elements'][$arr['elementhabar']]['timedrying'];
            $arr = $mantra->get('drier', $id, $arr);
            #return $arr;
            
            #return Habars::$habars[$arr['habar']]['elements'][$arr['elementhabar']]['timedrying'];
            #return $this->checkHabarOffDrier($arr['time'], Habars::$habars[$arr['habar']]['elements'][$arr['elementhabar']]['timedrying']);
            if($this->checkHabarOffDrier($arr['time'], Habars::$habars[$arr['habar']]['elements'][$arr['elementhabar']]['timedrying'])){
                   $this->clearHabar($arr['id']);
                   $ingridient->addIngrient($id, $arr['habar'], Habars::$habars[$arr['habar']]['type'], $arr['elementhabar']);
                   continue;    
            }
            #return Habars::$habars;
                                   
            $drier[$arr['elementhabar']]['element'] = $arr['elementhabar'];
            $drier[$arr['elementhabar']]['habar'] = $arr['habar'];
            $drier[$arr['elementhabar']]['name'] = Habars::$habars[$arr['habar']]['elements'][$arr['elementhabar']]['name'];
            $drier[$arr['elementhabar']]['image'] = Habars::$habars[$arr['habar']]['elements'][$arr['elementhabar']]['image'];
            $drier[$arr['elementhabar']]['descript'] = Habars::$habars[$arr['habar']]['elements'][$arr['elementhabar']]['descript'];                                       
            $drier[$arr['elementhabar']]['type'] = Habars::$habars[$arr['habar']]['type'];
            $drier[$arr['elementhabar']]['count']++;
            if($drier[$arr['elementhabar']]['time'])
            {
                $drier[$arr['elementhabar']]['time'] 
                    = min($drier[$arr['elementhabar']]['time'],
                    ($arr['time'] + Habars::$habars[$arr['habar']]['elements'][$arr['elementhabar']]['timedrying']) - time());

                if($arr['origintime']){
                    $drier[$arr['elementhabar']]['origintime'] 
                        = min($drier[$arr['elementhabar']]['origintime'],
                    ($arr['origintime'] + Habars::$habars[$arr['habar']]['elements'][$arr['elementhabar']]['timedrying']) - time());    
                }
            }else{
                $drier[$arr['elementhabar']]['time'] = ($arr['time'] + Habars::$habars[$arr['habar']]['elements'][$arr['elementhabar']]['timedrying']) - time();
                if($arr['origintime']){
                    $drier[$arr['elementhabar']]['origintime'] = ($arr['origintime'] + Habars::$habars[$arr['habar']]['elements'][$arr['elementhabar']]['timedrying']) - time();    
                }
            }
            $i++;    
        }         
        foreach($drier as $val){
            $_drier[] = $val;
        }                      
        #return $this->complete($_drier);
        return $_drier;
    }
    
    /**
     * Удаляем из сушилки
    */
    private function clearHabar($id){
        $sql = "DELETE FROM `".PREFIX_TABLE."drier` WHERE `id` = '".$id."'  LIMIT 1";
        if(!$result = DBConnect::query($sql)){
            return false;
        }
        return true;   
    }
    
    /**
     * Высушен ли хабар
    */
    private function checkHabarOffDrier($createTime, $timedrying){
        #return (intval( $createTime)  )." - ".time();
        #return ($createTime + $timedrying) - time();
        #return $timedrying; 
        if((intval($createTime) + intval($timedrying)) <= time()){
            return true;
        }
        return false;
        
    }
}
?>